home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / MacVogl-alpha1PPC / viewp.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  4KB  |  180 lines

  1. #include <stdio.h>
  2. #include "vogl.h"
  3.  
  4. static    Vstack    *vsfree = (Vstack *)NULL;
  5.  
  6. /*
  7.  * pushviewport
  8.  *
  9.  * pushes the current viewport on the viewport stack
  10.  *
  11.  */
  12. void
  13. pushviewport()
  14. {
  15.     Vstack    *nvport;
  16.     Token    *tok;
  17.  
  18.     if (!vdevice.initialised)
  19.         verror("pushviewport: vogl not initialised");
  20.     
  21.     if (vdevice.inobject) {
  22.         tok = newtokens(1);
  23.  
  24.         tok->i = PUSHVIEWPORT;
  25.  
  26.         return;
  27.     }
  28.  
  29.     if (vsfree != (Vstack *)NULL) {
  30.         nvport = vdevice.viewport;
  31.         vdevice.viewport = vsfree;
  32.         vsfree = vsfree->back;
  33.         vdevice.viewport->back = nvport;
  34.         vdevice.viewport->v.left = nvport->v.left;
  35.         vdevice.viewport->v.right = nvport->v.right;
  36.         vdevice.viewport->v.bottom = nvport->v.bottom;
  37.         vdevice.viewport->v.top = nvport->v.top;
  38.     } else {
  39.         nvport = (Vstack *)vallocate(sizeof(Vstack));
  40.         nvport->back = vdevice.viewport;
  41.         nvport->v.left = vdevice.viewport->v.left;
  42.         nvport->v.right = vdevice.viewport->v.right;
  43.         nvport->v.bottom = vdevice.viewport->v.bottom;
  44.         nvport->v.top = vdevice.viewport->v.top;
  45.         vdevice.viewport = nvport;
  46.     }
  47. }
  48.  
  49. /*
  50.  * popviewport
  51.  *
  52.  * pops the top viewport off the viewport stack.
  53.  *
  54.  */
  55. void
  56. popviewport()
  57. {
  58.     Token    *tok;
  59.     Vstack    *nvport;
  60.  
  61.     if (!vdevice.initialised)
  62.         verror("popviewport: vogl not initialised");
  63.     
  64.     if (vdevice.inobject) {
  65.         tok = newtokens(1);
  66.  
  67.         tok->i = POPVIEWPORT;
  68.  
  69.         return;
  70.     }
  71.  
  72.     if (vdevice.viewport->back == (Vstack *)NULL)
  73.         verror("popviewport: viewport stack underflow");
  74.     else {
  75.         nvport = vdevice.viewport;
  76.         vdevice.viewport = vdevice.viewport->back;
  77.         nvport->back = vsfree;
  78.         vsfree = nvport;
  79.     }
  80.  
  81.     vdevice.maxVx = vdevice.viewport->v.right * vdevice.sizeSx;
  82.     vdevice.maxVy = vdevice.viewport->v.top * vdevice.sizeSy;
  83.     vdevice.minVx = vdevice.viewport->v.left * vdevice.sizeSx;
  84.     vdevice.minVy = vdevice.viewport->v.bottom * vdevice.sizeSy;
  85.  
  86.     CalcW2Vcoeffs();
  87. }
  88.  
  89. /*
  90.  * viewport
  91.  *
  92.  * Define a Viewport in Normalized Device Coordinates
  93.  *
  94.  * The viewport defines that fraction of the screen that the window will
  95.  * be mapped onto. Unlike in VOGLE  the screen dimension is from 0 to sizeX
  96.  * and 0 to sizeY.
  97.  */
  98. void
  99. viewport(xlow, xhigh, ylow, yhigh)
  100.     Screencoord    xlow, xhigh, ylow, yhigh;
  101. {
  102.     Token    *tok;
  103.     char    buf[35];
  104.  
  105.     if (!vdevice.initialised) 
  106.         verror("viewport: vogl not initialised");
  107.  
  108.     /*
  109.      *    A few preliminary checks ....
  110.      */
  111.     
  112.     if (xlow < 0 || xhigh < 0) {
  113.         sprintf(buf,"viewport: x dimension value is invalid");
  114.         verror(buf);
  115.     } 
  116.  
  117.     if (ylow < 0 || yhigh < 0) {
  118.         sprintf(buf,"viewport: y dimension value is invalid");
  119.         verror(buf);
  120.     } 
  121.  
  122.     if (xlow >= xhigh) {
  123.         sprintf(buf,"viewport: xleft(%d) >= xright(%d)", xlow, xhigh);
  124.         verror(buf);
  125.     } 
  126.     if (ylow >= yhigh) {
  127.         sprintf(buf,"viewport: ybottom(%d) >= ytop(%d)", ylow, yhigh);
  128.         verror(buf);
  129.     } 
  130.  
  131.     if (vdevice.inobject) {
  132.         tok = newtokens(5);
  133.  
  134.         tok[0].i = VIEWPORT;
  135.         tok[1].i = xlow;
  136.         tok[2].i = xhigh;
  137.         tok[3].i = ylow;
  138.         tok[4].i = yhigh;
  139.  
  140.         return;
  141.     }
  142.  
  143.     if (xhigh >= vdevice.sizeSx)
  144.         xhigh = vdevice.sizeSx;
  145.  
  146.     if (yhigh >= vdevice.sizeSy)
  147.         yhigh = vdevice.sizeSy;
  148.  
  149.     /*
  150.      * Make sure the viewport stack knows about us.....
  151.      */
  152.     vdevice.viewport->v.left = xlow / (double)vdevice.sizeSx;
  153.     vdevice.viewport->v.right = xhigh / (double)vdevice.sizeSx;
  154.     vdevice.viewport->v.bottom = ylow / (double)vdevice.sizeSy;
  155.     vdevice.viewport->v.top = yhigh / (double)vdevice.sizeSy;
  156.  
  157.     vdevice.minVx = xlow;
  158.     vdevice.minVy = ylow;
  159.     vdevice.maxVx = xhigh;
  160.     vdevice.maxVy = yhigh;
  161.  
  162.     CalcW2Vcoeffs();
  163. }
  164.  
  165. /*
  166.  * getviewport
  167.  *
  168.  *    Returns the left, right, bottom and top limits of the current
  169.  *    viewport.
  170.  */
  171. void
  172. getviewport(left, right, bottom, top)
  173.     Screencoord    *left, *right, *bottom, *top;
  174. {
  175.     *right = vdevice.maxVx;
  176.     *top = vdevice.maxVy;
  177.     *left = vdevice.minVx;
  178.     *bottom = vdevice.minVy;
  179. }
  180.